<template>
    <div class="container addmusic loading">
        <main  class="table-div">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
              <el-form-item label="权限名称" prop="authname">
                <el-input v-model="form.authname"></el-input>
              </el-form-item>
               <el-form-item label="权限标识" prop="authid">
                <el-input v-model="form.authid"></el-input>
              </el-form-item>
               <el-form-item label="上级权限" prop="pid">
                <el-input readonly v-model="form.pauthname" placeholder="默认为顶级权限"></el-input>
                <input type="hidden" v-model="form.pid">
                <el-tree
                  :data="treeData.data"
                  :props="treeData.defaultProps"
                  accordion
                  @node-click="upPid"
                  :highlight-current='true'>
                </el-tree>
              </el-form-item>
               <el-form-item label="权限url" prop="authurl">
                <el-input v-model="form.authurl"></el-input>
              </el-form-item>
               <el-form-item label="权限类型" prop="authtype">
                 <el-select v-model="form.authtype" placeholder="请选择">
                  <el-option label="菜单权限" value="menu"></el-option>
                  <el-option label="功能权限" value="function"></el-option>
                  <!-- <el-option label="操作权限" value="button"></el-option> -->
                </el-select>
              </el-form-item>
              <el-form-item v-if="form.authtype=='function'" label="方法名" prop="funid">
                <el-input  v-model="form.funid"></el-input>
              </el-form-item>
              <el-form-item label="图标" prop="resico">
                 <el-input readonly v-model="form.resico" @click.native="isshowIcon=true"></el-input>
              </el-form-item>
              <el-form-item label="是否启用" prop="valid">
                <el-switch
                  v-model="form.valid"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="不启用"
                  :active-value="1"
                  :inactive-value="0">
                </el-switch>
              </el-form-item>
              <el-form-item label="排序" prop="authorder">
                <el-input  type='number' v-model="form.authorder"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button v-if="!id" type="primary" @click="submitAdd('ruleForm')">确认添加</el-button>
                <el-button v-if="id" type="primary" @click="submitEdit('ruleForm')">确认修改</el-button>
              </el-form-item>
            </el-form>
        </main>
        <el-dialog
          title="选择图标"
          :visible.sync="isshowIcon"
          width="60%"
          ref="iconpopup"
          >
          <icon-popup @changeIcon="changeIconName"></icon-popup>
        </el-dialog>
    </div>
</template>

<script>
import { getPermissionTree, getPermissionDetail, editPermission, addPermission } from '@/api/permission'

import iconPopup from '@/components/IconPopup'
export default {
  data () {
    return {
      isshowIcon: false,
      id: '',
      form: {
        authname: '',
        authid: '',
        authurl: '',
        authtype: '',
        pid: '',
        pauthname: '',
        valid: 1,
        authorder: 0,
        resico: '',
        funid: ''
      },
      rules: {
        authname: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        authid: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ],
        authurl: [
          { required: true, message: '请输入权限url', trigger: 'blur' }
        ],
        authtype: [
          { required: true, message: '请选择权限类型', trigger: 'blur' }
        ]
      },
      treeData: {
        data: [],
        defaultProps: {
          children: 'nodes',
          label: 'text'
        }
      }
    }
  },
  components: {
    iconPopup
  },
  methods: {
    changeIconName (e) {
      this.form.resico = e
      this.isshowIcon = false
    },
    upPid (...data) {
      this.form.pid = data[1].data.id
      this.form.pauthname = data[1].data.text
    },
    getTreeData () {
      getPermissionTree().then(data => {
        if (data.result === 1) {
          this.treeData.data = data.data
        }
      })
    },
    getDetails (id) {
      getPermissionDetail({ id: id }).then(data => {
        if (data.result === 1) {
          data.data.valid = data.data.valid ? 1 : 0
          Object.assign(this.form, data.data)
        } else {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      })
    },
    submitAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addPermission(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.back()
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    },
    submitEdit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.id = this.id
          editPermission(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.replace({ name: 'PermissionList', params: { page: this.oldPage }})
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    }
  },
  created () {
    this.getTreeData()
  },
  beforeMount () {
    this.id = this.$route.params.id
    this.oldPage = this.$route.params.page ? this.$route.params.page : ''
    if (this.id) this.getDetails(this.id)
  },
  mounted () {

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;
.table-div{
    width:90%;
    margin: 20px auto
}
.title{
    width:90%;
    margin: 10px auto;
    .search-div,.row-div{
      float: left;
    }
    .search-div{
      width: 200px;
      margin-right: 20px
    }
}
.el-upload-list__item{
  height:auto !important;
}
</style>
